﻿//Copyright (c) Microsoft Corporation.  All rights reserved.

using Microsoft.Ted.Wacel.TestConsole.Properties;
using Microsoft.Ted.Wacel;
using System;

namespace Microsoft.Ted.Wacel.TestConsole
{
    public class TableStorageCompressedCircularBufferTester
    {
        public static void BasicBufferBehavior()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1, Settings.Default.StorageConnectionString);
            buffer.Add("A");
            buffer.Add("B");
            buffer.Add("C");
            buffer.Add("D");
            buffer.Add("E");
            VisualTester.AreEqual("E", buffer[0]);
        }
        public static void BasicCompression()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1, Settings.Default.StorageConnectionString);
            buffer.Add(new string[] { "AAAAA", "BBBB", "CCC", "DD", "E" });
            string[] values = buffer.Get(0);
            VisualTester.AreEqual(5, values.Length);
            VisualTester.AreEqual("AAAAA", values[0]);
            VisualTester.AreEqual("BBBB", values[1]);
            VisualTester.AreEqual("CCC", values[2]);
            VisualTester.AreEqual("DD", values[3]);
            VisualTester.AreEqual("E", values[4]);
        }
        public static void BatchedSend()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1,Settings.Default.StorageConnectionString, batchSize: 5);
            buffer.Add("AAAAAAAA");
            buffer.Add("BBBBBBB");
            buffer.Add("");
            buffer.Add("DD");
            buffer.Add(";*;*;");
            string[] values = buffer.Get(0);
            VisualTester.AreEqual(5, values.Length);
            VisualTester.AreEqual("AAAAAAAA", values[0]);
            VisualTester.AreEqual("BBBBBBB", values[1]);
            VisualTester.AreEqual("", values[2]);
            VisualTester.AreEqual("DD", values[3]);
            VisualTester.AreEqual(";*;*;", values[4]);
        }
        public static void SingleGetWithoutDuplicationDetection()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1, Settings.Default.StorageConnectionString, batchSize: 5, avoidDuplicatedRead: false);
            buffer.Add("AAAAVAAAA");
            buffer.Add("BBBBBBB");
            buffer.Add("");
            buffer.Add("DD");
            buffer.Add(";*;*;");
            VisualTester.AreEqual("AAAAVAAAA", buffer[-4]);
            VisualTester.AreEqual("BBBBBBB", buffer[-3]);
            VisualTester.AreEqual("", buffer[-2]);
            VisualTester.AreEqual("DD", buffer[-1]);
            VisualTester.AreEqual(";*;*;", buffer[0]);
        }
        public static void SingleGetWithDuplicationDetection()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1, Settings.Default.StorageConnectionString, batchSize: 2);
            buffer.Add("AAAAVAAAA");
            buffer.Add("BBBBBBB");
            try
            {
                VisualTester.AreEqual("BBBBBBB", buffer[0]);
                VisualTester.Assert(false);
            }
            catch (InvalidOperationException)
            {
                VisualTester.Assert(true);
            }
            catch (Exception exp)
            {
                VisualTester.Assert(false, exp.Message);
            }
        }
        public static void ExplicitBatchFlush()
        {
            TableStorage.CompressedCircularBuffer buffer = new TableStorage.CompressedCircularBuffer("thankyou", 1, Settings.Default.StorageConnectionString, batchSize: 5);
            buffer.Add("AAAAVAAAA");
            buffer.Add("BBBBBBB");
            buffer.Flush();
            string[] values = buffer.Get(0);
            VisualTester.AreEqual(2, values.Length);
            VisualTester.AreEqual("AAAAVAAAA", values[0]);
            VisualTester.AreEqual("BBBBBBB", values[1]);
        }
    }
}
